<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title><?= htmlspecialchars($title ?? 'MyCMS') ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background: #f8f9fa;
        }

        .navbar {
            background: #fff;
            border-bottom: 1px solid #e9ecef;
            padding: 1rem 0;
            margin-bottom: 2rem;
        }

        .navbar .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .navbar .brand {
            font-size: 1.5rem;
            font-weight: bold;
            color: #007bff;
            text-decoration: none;
        }

        .navbar .nav-links {
            display: flex;
            gap: 1.5rem;
            align-items: center;
        }

        .navbar .nav-links a {
            color: #666;
            text-decoration: none;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            transition: all 0.2s;
        }

        .navbar .nav-links a:hover {
            background: #f8f9fa;
            color: #007bff;
        }

        .navbar .user-info {
            color: #666;
            font-size: 0.9rem;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
        }

        .alert {
            padding: 1rem;
            margin-bottom: 1.5rem;
            border-radius: 4px;
            border: 1px solid transparent;
        }

        .alert-success {
            color: #155724;
            background-color: #d4edda;
            border-color: #c3e6cb;
        }

        .alert-error {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }

        .alert-warning {
            color: #856404;
            background-color: #fff3cd;
            border-color: #ffeaa7;
        }

        .alert-info {
            color: #0c5460;
            background-color: #d1ecf1;
            border-color: #bee5eb;
        }

        .footer {
            margin-top: 3rem;
            padding: 2rem 0;
            background: #fff;
            border-top: 1px solid #e9ecef;
            text-align: center;
            color: #666;
            font-size: 0.9rem;
        }

        .btn {
            display: inline-block;
            padding: 0.6rem 1.2rem;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            font-size: 0.9rem;
            transition: background 0.2s;
        }

        .btn:hover {
            background: #0056b3;
        }

        .btn-secondary {
            background: #6c757d;
        }

        .btn-secondary:hover {
            background: #545b62;
        }

        .btn-danger {
            background: #dc3545;
        }

        .btn-danger:hover {
            background: #c82333;
        }
    </style>
</head>

<body>
    <nav class="navbar">
        <div class="container">
            <a href="/" class="brand">MyCMS</a>
            <div class="nav-links">
                <a href="/">首页</a>
                <a href="/posts">文章</a>
                <?php
                // 获取当前用户信息
                $currentUser = null;
                try {
                    $currentUser = App\Services\Auth::user();
                } catch (Exception $e) {
                    // 忽略错误，用户未登录
                }

                if ($currentUser): ?>
                    <a href="/posts/create">写文章</a>
                    <a href="/admin">管理面板</a>
                    <span class="user-info">
                        欢迎，<?= htmlspecialchars($currentUser['username'] ?? '用户') ?>
                    </span>
                    <a href="/logout">退出</a>
                <?php else: ?>
                    <a href="/login">登录</a>
                    <a href="/register">注册</a>
                <?php endif; ?>
            </div>
        </div>
    </nav>

    <div class="container">
        <?php
        // 显示闪现消息
        $flashTypes = ['success', 'error', 'warning', 'info'];
        foreach ($flashTypes as $type) {
            try {
                if (App\Core\Flash::has($type)) {
                    $message = App\Core\Flash::get($type);
                    echo '<div class="alert alert-' . htmlspecialchars($type) . '">';
                    echo htmlspecialchars($message);
                    echo '</div>';
                }
            } catch (Exception $e) {
                // 忽略Flash消息错误
            }
        }
        ?>

        <?= $content ?? '' ?>
    </div>

    <footer class="footer">
        <div class="container">
            <p>&copy; <?= date('Y') ?> MyCMS. 基于 PHP 构建的轻量内容管理系统。</p>
        </div>
    </footer>
</body>

</html>